package com.cms.work.controller.sys;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.cms.work.core.SimpleResponse;
import com.cms.work.core.controller.BaseController;
import com.cms.work.entity.enums.CommonEnum.ResourceStatusEnum;
import com.cms.work.entity.enums.CommonEnum.ResourceTypeEnum;
import com.cms.work.entity.enums.CommonEnum.UserStatusEnum;
import com.cms.work.entity.sys.SysResource;
import com.cms.work.entity.sys.vo.ResourceRelationVo;
import com.cms.work.service.sys.ISysResourceService;
import com.cms.work.service.sys.ISysRoleResourceService;
import com.github.pagehelper.PageInfo;

/**
 * <p>Title: SysResourceController</p>
 * <p>Description: 资源控制层</p>
 * <p>Email: yuyue_qty@163.com</p>
 * @author 晴天雨
 * @date 2016年11月23日下午11:01:25
 * @version 1.0
 */
@Controller
@RequestMapping("/sys")
public class SysResourceController extends BaseController<SysResource> {

	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	@Autowired
	private ISysResourceService sysResourceService;
	@Autowired
	private ISysRoleResourceService sysRoleResourceService;
	
	@RequestMapping(value="/queryResource", method={RequestMethod.POST, RequestMethod.GET})
	public ModelAndView query(
			@RequestParam(value="pageNum", defaultValue=PAGE_NUM)int pageNum,
			@RequestParam(value="pageSize", defaultValue=PAGE_SIZE)int pageSize,
			SysResource sysResource){
		if(StringUtils.isBlank(sysResource.getName())) sysResource.setName(null);
		PageInfo<SysResource> pageInfo = sysResourceService.fuzzySelectPage(pageNum, pageSize, "create_date desc, sort", sysResource);
		return new ModelAndView("/sys/resource-list").addObject("pageInfo", pageInfo);
	}
	
	@RequestMapping(value="/loadResource", method={RequestMethod.GET})
	public ModelAndView load(@RequestParam(value="resourceId", defaultValue="0")int id){
		List<ResourceRelationVo> rootResourceList = null;
		SysResource resource = sysResourceService.selectByPrimaryKey(id);
		if(resource == null){
			resource = new SysResource();
			rootResourceList = sysResourceService.getResourceRelation(null);
		}else{
			rootResourceList = sysResourceService.getResourceRelation(resource.getPid());
		}
		resource.setMaxSort(sysResourceService.getMaxSort());
		return new ModelAndView("/sys/resource-add").addObject("resource", resource).addObject("rootResourceList", JSON.toJSONString(rootResourceList));
	}
	
	@RequestMapping(value="/loadParentResource", method=RequestMethod.GET)
	@ResponseBody
	public List<SysResource> loadParentResource(int pid){
		return sysResourceService.selectByCondition(new SysResource(pid, ResourceTypeEnum.menu.getType(), 
				ResourceStatusEnum.enabled.getStatus()));
	}
	
	@RequestMapping(value="/addResource", method={RequestMethod.POST})
	public String add(SysResource resource, Integer[] pids){
		if(pids != null && pids.length > 0){
			resource.setPid(pids[pids.length-1]);
		}
		resource.setCreateDate(new Date());
		sysResourceService.save(resource);
		return "redirect:/sys/queryResource?id="+resource.getId();
	}
	
	@RequestMapping(value="/changeResourceStatus", method={RequestMethod.POST, RequestMethod.GET})
	@ResponseBody
	public SimpleResponse changeStatus(@RequestParam(value="resourceId")int id, int status){
		SimpleResponse result = new SimpleResponse();
		try {
			if(status == UserStatusEnum.disable.getStatus()){
				sysResourceService.updateResourceStatus(id, ResourceStatusEnum.disable);
			}else if(status == UserStatusEnum.enabled.getStatus()){
				sysResourceService.updateResourceStatus(id, ResourceStatusEnum.enabled);
			}else{
				result.setMsg("用户资源状态未定义");
				result.setState(SimpleResponse.StateEnum.UNSIGN.getValue());
				return result;
			}
			result.putData("resource", sysResourceService.selectByPrimaryKey(id));
			result.setState(SimpleResponse.StateEnum.SUCCESS.getValue());
			result.setMsg("success");
		} catch (Exception e) {
			result.setMsg(e.getMessage());
			result.setState(SimpleResponse.StateEnum.ERROR.getValue());
			logger.error("用户资源状态修改失败", e);
		}
		return result;
	}
}










